<?php

namespace App\Common\Services\Admin;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Redis;

class Common
{
    /**
     * 获取当前登录人员详情
     */
    public static function getCurrentAdmininfo(Request $request)
    {
        return self::getTokenInfo($request);
    }


    /**
     * 获取当前登录人员公司ID
     */
    public static function getCurrentAdminId(Request $request)
    {
        return intval(self::getTokenInfo($request, 'id'));
    }
    /**
     * 从token提取信息
     */
    private static function getTokenInfo($request, $key = '')
    {
        $token = $request->header('admintoken');
        $accountinfo = Redis::get($token);
        $accountinfo = $accountinfo ? json_decode($accountinfo, true) : '';
        if ($key) {
            if (isset($accountinfo[$key])) {
                return $accountinfo[$key];
            } else {
                return '';
            }
        } else {
            return $accountinfo;
        }
    }


    /**
     * 存储超管token集合
     */
    public static function storetokenkey($token,$adminid){
        $token_list=Redis::get('admin_user_token_list_'.$adminid);
        $token_list_arr=$token_list?json_decode($token_list,true):[];
        $token_list_arr[]=$token;
        Redis::set('admin_user_token_list_'.$adminid,json_encode($token_list_arr));
    }

    /**
     * 清除超管token集合，常用语修改密码后
     */
    public static function cleartokenkey($adminid){
        $token_list=Redis::get('admin_user_token_list_'.$adminid);
        $token_list_arr=$token_list?json_decode($token_list,true):[];
        foreach($token_list_arr as $val){
            Redis::delete($val);
        }
        Redis::delete('admin_user_token_list_'.$adminid);

    }
}
